<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class PhotoController extends Controller
{


   public function getAdd()
   {
      // $scenic = DB::table('scenic')->lists('name');
      $cates = DB::table('scenic')->get();
      // dd($scenic);
      return view('admin.photo.add',['cates'=>$cates]);
	
	 }
     // ----------------------------------------------------------商品添加
  public function postInsert(Request $request)
  {
    // dd($request->all());

    // $id = $request->only(['pid']);
       

        $data = $request->only(['stitle','contents','sid']);
       //添加图片
         if($request->hasFile('photo')){
            //随机文件名
            $name = time()+rand(1,99);

             //获取后缀名
            $ext = $request->file('photo')->getClientOriginalExtension();
            // echo $name.'.'.$ext;
            //检测后缀名
            $arr = array('jpg','png','jpeg');
            //判断
            if(!in_array($ext,$arr)){
                echo '<script type="text/javascript">
                    alert("上传文件不合法");
                    location.href="http://la.cn/test/add";
                </script>';
            }
            $request->file('photo')->move('./public/uploads/',$name.'.'.$ext);
            // echo '<img src="public/uploads/',$name.'.'.$ext.'" alt="">';
           
            $img = self::suofang('./public/uploads/'.$name.'.'.$ext,100,100);
            // dd($data['pic']);
            $data['photo'] = $name.'.'.$ext;
          


            

        }

      //执行数据插入
      $res = DB::table('scenic_photo')->insert($data);
    
      //判断是否添加成功
      if($res){
        // return redirect('/admin/photo/add')->with('success','用户添加成功');
        return redirect('admin/photo/list');
      }else{
        return back()->with('error','用户添加失败');
     
      }
    }

   


// -----------------------------------------------------商品列表
    public function getList(Request $request){
      
   // echo '用户列表页';
   //查询所有图册数据 
   $cates = DB::table('scenic')->get();
   // return view('admin.photo.add',['cates'=>$cates]);
   //获取分页大小
   // $num = $request->input('num',5);
   $num = 5;
   //判断是否有选择景点
   if($request->input('sid')){
     $pho = DB::table('scenic_photo')->where('sid',$request->input('sid'))->paginate($num);
   }else{
     $pho = DB::table('scenic_photo')->where('sid',1)->paginate($num);
   }
   //获取当前页面接收到的所有参数
   $data = $request->all();
    // var_dump($data);

   //解析模板
   return view('admin.photo.list',['pho'=>$pho,'request'=>$data],['cates'=>$cates]);
 }

// // // --------------------------------------------------------商品删除
 public  function getDelete(Request $request){
         // dd($request -> input('id'));
       $id = $request -> input('id');

       $res = DB::table('scenic_photo')->where('id',$id)->delete();
       if($res){
            echo 1;
       }else{
         echo 2;
       }
     }
  
// // // ----------------------------------------------------商品修改

   public function getEdit($id){
             
      $scenic = DB::table('scenic_photo')->where('id',$id)->first();
      
      return view('admin.photo.edit',['scenic'=>$scenic]);
    
     }


     public function postUpdate(Request $request){
       // dd($request -> all());
      $id = $request->only(['id']);

      $data = $request->only(['stitle','contents']);
     
             //修改图片
            //获取图片
         if($request->hasFile('photo')){
            //随机文件名
            $name = time()+rand(1,99);

             //获取后缀名
            $ext = $request->file('photo')->getClientOriginalExtension();
            // echo $name.'.'.$ext;
            //检测后缀名
            $arr = array('jpg','png','jpeg');
            //判断
            if(!in_array($ext,$arr)){
                echo '<script type="text/javascript">
                    alert("上传文件不合法");
                    location.href="http://la.cn/test/add";
                </script>';
            }
            $request->file('photo')->move('./public/uploads/',$name.'.'.$ext);
            // echo '<img src="public/uploads/',$name.'.'.$ext.'" alt="">';
           
            $img = self::suofang('./public/uploads/'.$name.'.'.$ext,100,100);
            // dd($data['pic']);
            $data['photo'] = $name.'.'.$ext;   

        }

        //执行修改
         $res = DB::table('scenic_photo')->where('id',$id)->update($data);
      // var_dump($res);
       if($res){
            return redirect('/admin/photo/list')->with('success','用户修改成功');

        }else{
          return back()->with('error','用户修改失败');
        }
   
     }




         //缩放图片
           public static function suofang($spath, $sw = 100, $sh = 100, $pre = 's_')
          {
          //1.创建画布
          //计算比例  100 100  要缩放的图像的宽高  1920 1200  //用小的比例
          $srcinfo = getimagesize($spath);
          //根据图片类型 去创建图像资源 ...
          $src = imagecreatefromjpeg($spath); //strrchr  aaaa.jpg
          
          if($sw/$srcinfo[0] > $sh/$srcinfo[1]){
              $bili = $sh/$srcinfo[1];
          }else{
              $bili = $sw/$srcinfo[0];
          }
          //舍去法取整
          $sw = floor($srcinfo[0] * $bili);
          $sh = floor($srcinfo[1] * $bili);


          $dst = imagecreatetruecolor($sw,$sh);

          //3.开始绘画
          imagecopyresampled($dst, $src, 0, 0, 0, 0, $sw, $sh, $srcinfo[0], $srcinfo[1]);

          //5.将图像保存为文件
          //随机文件名  //获取文件后缀名  目录
          //$ext = strrchr($spath,'.');
          //$filename = time().rand(1000,9999).$ext;
          $filename = rtrim(dirname($spath),'/').'/'.$pre.basename($spath);
          
          imagejpeg($dst, $filename);

          //6.销毁资源
          imagedestroy($dst);
          imagedestroy($src);
          
          //返回原文件名加前缀
          return $pre.basename($spath);
      }

}
